import React, {useState,useEffect} from 'react';
import { useHistory ,Link} from "react-router-dom";
import http from "../../model/http";
import {Button, Toast,Picker,Flex,List} from 'antd-mobile';
import './login.less'
import Input from "../../components/input/Input";
import number from "less/lib/less/functions/number";
import code from "../../model/code";


const Item = List.Item
function Login() {
    const history = useHistory();
    const [codeImg,setCodeImg] = useState({})
    const [codeValue,setCodeValue] = useState()
    const [username,setUsername] = useState()
    const [password,setPassword] = useState()

    // 下拉展示的数据
    const [hosList,setHosList] = useState([ {label:'曙光医美',value:'1'},{label:'曙光口腔',value:'2'}  ])
    // 选中的值
    const [hosValue,serHosValue] =useState (['1'])

    let url =window.location.href
    // console.log("url",url)
    // let gotoType = url.substring(url.lastIndexOf("/")+1);
    useEffect( async ()=>{
        // window.location.reload()
        let loginStatus = sessionStorage.getItem('loginStatus') ? sessionStorage.getItem('loginStatus') : '0';
        if(loginStatus == "1"){
            window.history.go(-1)
        }else{
            window.localStorage.removeItem('user')
            const user = await code.getUser();
            window.localStorage.setItem('user', JSON.stringify(user))
        }

        
    },[])

    const changeUsername=(e)=> {
        setUsername(e.target.value)
    }
    const changePassword=(e)=> { 
        setPassword(e.target.value)
    }

    // //登录并绑定接口
    // const onBind =async()=>{
    //    const user =JSON.parse(window.localStorage.getItem('user'))
    //     const res = await http.outPost('/arsmo/wxworkBind', {
    //         account:username,
    //         password:password,
    //         userId: user.userid,
    //         hosId: user.hosId
    //     })
    //     console.log(res)
    //     if(res.code==0){
    //         Toast.success(res.msg,2, async ()=>{
    //             if(url.indexOf('/user/home')>-1){
    //                 window.location.href = '/user/home'
    //             }else{
    //                 window.location.href = '/user'
    //             }
    //         })
    //     }else{
    //         Toast.fail(res.msg,2)
    //     }
    // }
    const onBind =async()=>{
        const user =JSON.parse(window.localStorage.getItem('user'))

      



        const res = await http.outPost('/arsmo/wxworkBind', {
            account:username,
            password:password,
            userId: user.userid,
            // userId: 'SG03133', // 暂时写死
            hosId: hosValue
        })
        console.log(res)
        if(res.code==0){


            Toast.success(res.msg,1.5, async ()=>{
                // 被系统删除之后，由于缓存在本地，需要更新
                window.localStorage.setItem('hosId',hosValue)


                const check =await code.checkBind()
                sessionStorage.setItem('loginStatus','1')
                console.log("dddddddd",check) 
                if(url.indexOf('/user/home')>-1){
                    window.location.href = '/user/home'
                }else if(url.indexOf('/yuyue')>-1){
                    window.location.href = '/yuyue'
                }else if(url.indexOf('/billing')>-1){
                    window.location.href = '/billing'
                }else{
                    window.location.href = '/user'
                }
            })
        }else{
            Toast.fail(res.msg,2)
        }
    }

    return (
    <div className='login'>
        <div className='form-wrapper'>
            <div className='title'> 曙光医疗管理系统</div>
            <div className='g-flex-col'>
                <input type="text" autofocus="autofocus" className='g-mt20' value={username} maxLength="20"  onChange={changeUsername.bind(this)}  placeholder='请输入账号'/>
                <input type="text" className='g-mt20' value={password} maxLength="20" onChange={changePassword}  placeholder='请输入密码'/>
                
                <Picker
                    cols={1}
                    title="请选择"
                    data={hosList}
                    value={hosValue}
                    onOk={(v) => { serHosValue(v)   }}
                    >
                    <Item arrow="horizontal"><Flex>  </Flex></Item>
                </Picker>
              
              
              
              
              
                {/*<div className='g-flex g-mt20 '>*/}
                {/*    <input type="text" className='code-input' value={codeValue}  onChange={change.bind(this)} placeholder='输入验证码'/>*/}
                {/*    {*/}
                {/*        codeImg?.img&&( <img onClick={reLoadImg} className='code-img' src={'data:image/jpg;base64,'+codeImg.img} alt=""/>)*/}
                {/*    }*/}
                {/*</div>*/}


                <Button className='login-bt' type="primary"  onClick={onBind}>立即绑定</Button>
            </div>
        </div>
    </div>
  )
}

export default Login;

